﻿using System.Collections.Specialized;
using System.Data.Entity;
using System.Web.Configuration;
using Df.Common.Forms;

namespace Df.Common.Model
{
    public class BaseDataContext : DbContext
    {

        public BaseDataContext(string connectionString)
            : base(connectionString)
        {

        }
        public BaseDataContext()
            : base((WebConfigurationManager.GetSection("jumbula") as NameValueCollection)["AzureDbConnection"]) // note: cannot use the helper here, see Helpers.cs
        {
            this.Configuration.LazyLoadingEnabled = true;
            this.Configuration.ProxyCreationEnabled = true;
        }

        public DbSet<DForm> DfForms { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<DForm>().ToTable("DfForms");
            base.OnModelCreating(modelBuilder);
        }


        //public override int SaveChanges()
        //{
        //    var modifiedEntries = this.ChangeTracker.Entries().Where(x => x.State == EntityState.Added || x.State == EntityState.Modified);

        //    foreach (var dbEntityEntry in modifiedEntries)
        //    {
        //        if (dbEntityEntry.Entity is DfBaseEntity)
        //        {
        //            var jsonData = JsonConvert.SerializeObject(dbEntityEntry.Entity);
        //            dbEntityEntry.Property("JsonData").CurrentValue = jsonData;
        //        }
        //    }
        //    return base.SaveChanges();
        //}

    }
}
